<template>
  <div class="app-container">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <el-input v-model="filter.name" placeholder="角色名称" size="small" clearable />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-button type="primary" size="small" @click="getList()">查 询</el-button>
          <el-button type="primary" size="small" @click="handleAdd()">新 增</el-button>
          <el-button size="small" @click="app.reset('filter')">清 空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="角色名" min-width="120px" prop="name" />
      <el-table-column label="状态" align="center" width="80px" prop="state">
        <template slot-scope="{row}">
          {{ row.state === 1 ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" width="150px" prop="oper_time" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item :disabled="row.id === '1' || row.id === '0' ? true : false" @click.native="handleEdit(row)">编 辑</el-dropdown-item>
              <el-dropdown-item :disabled="row.id === '1' ? true : false" @click.native="handlePower(row)">权 限</el-dropdown-item>
              <el-dropdown-item :disabled="row.id === '1' || row.id === '0' ? true : false" @click.native="handleDelete(row)">删 除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <PowerDialog ref="powerDialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { list, deleteTo } from '@/api/role'
import Pagination from '@/components/Pagination'
import Dialog from './components/role-dialog'
import PowerDialog from './components/power-dialog'

export default {
  name: 'Role',
  components: { Pagination, Dialog, PowerDialog },
  inject: ['app'],
  data() {
    return {
      list: null,
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      this.app.loading = true
      list({
        ...this.filter,
        ...e
      }).then(response => {
        this.list = response.role
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(error => {
        this.app.removeLoding()
        this.$message.error(error.msg ? error.msg : '操作失败')
      })
    },
    handlePower(row) {
      this.dialog.title = '权限设置'
      this.app.dialogParam(this.dialog.edit, ['id', 'p_id'], row)
      this.$refs['powerDialog'].visible = true
    },
    handleDelete(row) {
      this.$confirm('此操作将删除用户，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        deleteTo({
          ids: row['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleEdit(row) {
      this.dialog.title = '编辑角色'
      this.dialog.type = 'edit'
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'state'], row)
      this.$refs['dialog'].visible = true
    },
    handleAdd() {
      this.dialog.title = '新增角色'
      this.dialog.type = 'add'
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'state'])
      this.$refs['dialog'].visible = true
    }
  }
}
</script>
